<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Content_model extends CI_Model {

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
		$this->load->database();
	}

	function showallcontent_catetype($type,$cate){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->where(array("cateid"=>$cate,"typeid"=>$type));
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function analys_title($title=null){
		$this->load->database();
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				if(mb_strtolower(url_title($this->removesign($result->namecontent))) == $title){
					return $result->id;
				}
			}
		}
	}

	function get_cate_app($id){
		$this->load->database();
		$this->db->where('id',$id);
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				return $result->cateid;
			}
		}
	}

	function show_relate_app($cateid,$id){
		$this->load->database();
		$this->db->where('cateid',$cateid);
		$this->db->where_not_in('id',$id);
		$this->db->limit(4);
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function analys_title_to_id($title=null){
		$this->load->database();
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				if(mb_strtolower(url_title($this->removesign($result->namecontent))) == $title){
					return $result->id;
				}
			}
		}
	}

	function analys_title_to_cateid($title=null){
		$this->load->database();
		$query=$this->db->get('tbcatecontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				if(mb_strtolower(url_title($this->removesign($result->catename))) == $title){
					return $result->id;
				}
			}
		}
	}

	function analys_title_to_type($title=null){
		$this->load->database();
		$query=$this->db->get('tbtype');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $result){
				if(mb_strtolower(url_title($this->removesign($result->nametype))) == $title){
					return $result->id;
				}
			}
		}
	}

	function read_article($id){
		$this->load->database();
		$this->db->where('id',$id);
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_suggestion_app(){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$this->db->limit(10);
		$this->db->where(array("needhave"=>1));
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}



	function update_countlog($id){
		$this->load->database();
		$this->db->where('id',$id);
		$count = 0;
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $rows){
				$count = $rows->countlog;
			}
		}
		$this->db->query('UPDATE tbcontent SET countlog='.($count + 1).' WHERE id='.$id);
	}

	function update_liked($id){
		$this->load->database();
		$this->db->where('id',$id);
		$likelog = 0;
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $rows){
				$likelog = $rows->likelog;
			}
		}
		$this->db->query('UPDATE tbcontent SET likelog='.($likelog + 1).' WHERE id='.$id);
	}

	function show_allcontent($num,$offset){
		$this->load->database();
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_all_downlog($num,$offset){
		$this->load->database();
		$this->db->order_by("downlog", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_all_viewlog($num,$offset){
		$this->load->database();
		$this->db->order_by("countlog", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_allcontentbycate($cateid,$num,$offset){
		$this->load->database();
		$this->db->where("cateid",$cateid);
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_allcontentbytype($typeid,$num,$offset){
		$this->load->database();
		$this->db->where("typeid",$typeid);
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function show_detailscontent($id){
		$this->load->database();
		$this->db->where('id',$id);
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	function showcontent_bycate($cateid,$num,$offset){
		$this->load->database();
		$this->db->where('cateid',$cateid);
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function showcontent_bytype($typeid,$num,$offset){
		$this->load->database();
		$this->db->where('typeid',$typeid);
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function showcontent_bytypecate($typeid,$cateid,$num,$offset){
		$this->load->database();
		$this->db->where(array('typeid'=>$typeid,'cateid'=>$cateid));
		$this->db->order_by("id", "desc");
		$query=$this->db->get('tbcontent',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}

	// ham tinh tong noi dung theo tung hang muc
	function totalcontent(){
		$this->load->database();
		return $this->db->count_all('tbcatecontent');
	}

	function totalcontentbycate($cateid){
		$this->load->database();
		$this->db->where('cateid',$cateid);
		return $this->db->count_all('tbcatecontent');
	}

	function totalcontentbytype($typeid){
		$this->load->database();
		$this->db->where('typeid',$typeid);
		return $this->db->count_all('tbtype');
	}

	function totalbycate($cateid){
		$this->load->database();
		$this->db->where(array( 'cateid'=>$cateid));
		return $this->db->count_all('tbcatecontent');
	}
	function totalappactive(){
		$this->load->database();
		$query = $this->db->get_where('tbcontent', array('active'=>1));
		$count = $query->num_rows();
		return $count;
	}
	function totalappunactive(){
		$this->load->database();
		$query = $this->db->get_where('tbcontent', array('active'=>0));
		$count = $query->num_rows();
		return $count;
	}
	function totalbytype($typeid){
		$this->load->database();
		$this->db->where(array('typeid'=>$typeid ));
		return $this->db->count_all('tbcatecontent');
	}
	function totalbycatetype($typeid,$cateid){
		$this->load->database();
		$this->db->where(array('typeid'=>$typeid,'cateid'=>$cateid));
		return $this->db->count_all('tbcatecontent');
	}
	// ---------------KET THUC ham tinh tong noi dung ------------

	function changestatustype($id=null,$active=null){
		$this->load->database();
		$data = array('active'=>$active);
		$this->db->where('id',$id);
		$this->db->update('tbcontent',$data);
	}



	function add_newcontent($cateid,$typeid,$namecontent,$description,$images,$coins,$ifiles,$ifiles2,$ifiles3,$active,$uid){
		 
		$this->load->database();
		$data = array(
				'cateid'=>$cateid,
				'typeid'=>$typeid,
				'namecontent'=>$namecontent,
				'description'=>$description,
				'images'=>$images,
				'coins' =>$coins,
				'ifiles'=>$ifiles,
				'ifiles_2'=>$ifiles2,
				'ifiles_3'=>$ifiles3,
				'active'=>$active,
				'datecreated'=>date("d-m-Y H:i:s"),
				'userid'=>$uid
				 
		);
		$this->db->insert('tbcontent', $data);
	}

	function changestatusneedhave($id=null,$needhave=null){
		$this->load->database();
		$data = array('needhave'=>$needhave);
		$this->db->where('id',$id);
		$this->db->update('tbcontent',$data);
	}


	function update_content($id,$cateid,$typeid,$namecontent,$description,$images,$coins,$ifiles,$ifiles2,$ifiles3,$active){
		$this->load->database();
			
		if($images <> null){
			if($ifiles <> null || $ifiles2 <> null || $ifiles3 <> null)
				$data = array(
						'cateid'=>$cateid,
						'typeid'=>$typeid,
						'namecontent'=>$namecontent,
						'description'=>$description,
						'images'=>$images,
						'coins' =>$coins,
						'ifiles'=>$ifiles,
						'ifiles_2'=>$ifiles2,
						'ifiles_3'=>$ifiles3,
						'active'=>$active,
						'datecreated'=>date("d-m-Y H:i:s")
				);
			else{
				$data = array(
						'cateid'=>$cateid,
						'typeid'=>$typeid,
						'namecontent'=>$namecontent,
						'description'=>$description,
						'images'=>$images,
						'coins' =>$coins,
						'active'=>$active,
						'datecreated'=>date("d-m-Y H:i:s")
				);
			}
		}
		else{
			if($ifiles <> null || $ifiles2 <> null || $ifiles3 <> null)
				$data = array(
						'cateid'=>$cateid,
						'typeid'=>$typeid,
						'namecontent'=>$namecontent,
						'description'=>$description,
						'coins' =>$coins,
						'ifiles'=>$ifiles,
						'ifiles_2'=>$ifiles2,
						'ifiles_3'=>$ifiles3,
						'active'=>$active,
						'datecreated'=>date("d-m-Y H:i:s")

				);
			else{
				$data = array(
						'cateid'=>$cateid,
						'typeid'=>$typeid,
						'namecontent'=>$namecontent,
						'description'=>$description,
						'coins' =>$coins,
						'active'=>$active,
						'datecreated'=>date("d-m-Y H:i:s")
				);
			}

		}
		$this->db->where('id', $id);
		$this->db->update('tbcontent', $data);
	}


	function del_content($id){
		$this->load->database();
		$this->db->where('id', $id);
		$this->db->delete('tbcontent');
	}

	function removesign($str)
	{
		$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
				,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à");
		$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a");
		return str_replace($coDau,$khongDau,$str);
	}
	function get_by_user_id($user_id) {
		$this->db->order_by("id", "desc");
		$this->db->where("userid",$user_id);
		$query=$this->db->get('tbcontent');
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	function add_app($data = null,$id = null) {
		if ($id == null) {
			$this->db->insert('tbcontent', $data);
			$new_id = $this->db->insert_id();
			return $new_id;
		}
		else {
			$this->db->where("id",$id);
			$this->db->update('tbcontent', $data);
		}
	}
	function update_version($app_id, $app_file, $change_log, $version_name) {
		$data = array('app_id' => $app_id,'change_log'=>$change_log,'version_name'=>$version_name,'app_file'=>$app_file,'active'=>1,'date_created'=>date("d-m-Y H:i:s"));
		$this->db->insert('tbl_app_version', $data);	 
	}
}